﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AdminSystem.Lib.Utility
{
    public class TimeConverter
    {
        private static string stage1 = "刚刚";
        private static string stage2 = "{0}分钟前";
        private static string stage3 = "今天{0}点{1}分";
        private static string stage4 = "昨天{0}点";
        private static string stage5 = "前天{0}点";
        private static string stage6 = "{0}月{1}日";
        private static string stage7 = "{0}年{1}月{2}日";

        /// <summary>获取给定时间的字符串表达式
        /// 3分钟之内               	刚刚 
        /// 3分钟之外 1小时内   	    *分钟前
        /// 1小时之外 今天内     	    今天**点**分
        /// 今天之外                 	昨天*点
        /// 昨天之外                 	前天*点
        /// 前天之外  今年内      	    **月**日
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetString(DateTime dt)
        {
            TimeSpan ts = DateTime.Now - dt;

            if (ts.TotalMinutes < 3)
                return stage1;

            else if (ts.TotalHours < 1)
                return string.Format(stage2, (int)ts.TotalMinutes);

            else if (dt > DateTime.Today)
                return string.Format(stage3, TimeToDayFragement(dt.Hour), dt.Minute);

            else if (dt > DateTime.Today.AddDays(-1))
                return string.Format(stage4, TimeToDayFragement(dt.Hour));

            else if (dt > DateTime.Today.AddDays(-2))
                return string.Format(stage5, TimeToDayFragement(dt.Hour));

            else if (dt > new DateTime(DateTime.Now.Year, 1, 1))
                return string.Format(stage6, dt.Month, dt.Day);
            else
                return string.Format(stage7, dt.Year, dt.Month, dt.Day);
        }

        /// <summary>把时间转换成当天中的字段加时间表示式
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string TimeToDayFragement(int time)
        {
            if (time < 4)
                return string.Format("深夜{0}", time == 0 ? 12 : time);
            else if (time <= 4 && time >= 6)
                return string.Format("凌晨{0}", time);
            else if (time < 10)
                return string.Format("上午{0}", time);
            else if (time <= 13)
                return string.Format("中午{0}", time);
            else if (time <= 18)
                return string.Format("下午{0}", time - 12);
            else
                return string.Format("晚上{0}", time - 12);
        }
    }
}
